﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;

namespace YouHP.Pages.User.Group
{
    public class Create : YouHP.Pages.User.UserBasePage
    {
        protected GroupInfo GroupData { get; set; }
        protected GroupUserInfo groupuser = new GroupUserInfo();
        protected List<DistrictInfo> ProvinceList = new List<DistrictInfo>();
        protected List<string> mPingYin = new List<string>();


        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected void HandleCommon()
        {
            this.AddLocation("创建圈子", "/User/Group/Create.aspx");

            ProvinceList = DistrictBLL.Instance.GetDistrictListByParent(0);
            for (char c = 'A'; c <= 'Z'; c++)
            {
                mPingYin.Add(c.ToString());
            }

        }

        protected void HandlePost()
        {
            GroupInfo info = ValidateForm();
            if (info == null) return;
            bool succ = GroupBLL.Instance.AddGroup(info);
            //得到新创建的圈子
            GroupData = info;

            if (succ)
            {

                this.AddMsgLine("创建圈子成功，你可以进行如下操作！");
                return;
            }
            else
            {
                this.AddErrLine("操作失败！");
                return;
            }
        }

        protected void HandleGet()
        {

        }

        GroupInfo ValidateForm()
        {
            GroupInfo info = new GroupInfo();
            info.AppUserId = UserData.Id;
            info.AppUserName = UserData.UserName;
            info.GroupName = RequestTools.GetFormString("groupname").Trim();
            info.Logo = "/support/alpha/group.jpg";//默认头像，待确定
            info.CreatTime = DateTime.Now;
            info.ProvinceId = RequestTools.GetFormInt("province", 0);
            info.CityId = RequestTools.GetFormInt("city", 0);
            info.ProvinceName = DistrictBLL.Instance.GetDistrict(info.ProvinceId).Name;
            info.CityName = DistrictBLL.Instance.GetDistrict(info.CityId).Name;
            info.DiseaseId = RequestTools.GetFormInt("diseaseId", 0);
            if (info.GroupName.Length == 0)
            {
                this.AddErrLine("圈子名称不能为空！");
                return null;
            }
            if (info.GroupName.Length > 15)
            {
                this.AddErrLine("圈子名称不能大于15个字符！");
                return null;
            }

            info.Keyword = RequestTools.GetFormString("groupkeyword").Trim();
            if (info.Keyword.Length >10)
            {
                this.AddErrLine("圈子关键词不能超过10个字符！");
                return null;
            }
            info.Introduction = RequestTools.GetFormString("groupintro").Trim();
            if (info.Introduction.Length > 20)
            {
                this.AddErrLine("圈子介绍不能超过20个字符！");
                return null;
            }
            info.BrowserPermission = RequestTools.GetFormInt("groupbrowser", Constants.BrowserOpen);
            if (info.BrowserPermission != Constants.BrowserOpen && info.BrowserPermission != Constants.BrowserLimit)
                info.BrowserPermission = Constants.BrowserOpen;
            info.JoinPermission = RequestTools.GetFormInt("groupjoin", Constants.JoinOpen);
            if (info.JoinPermission != Constants.JoinOpen && info.JoinPermission != Constants.JoinLimit)
                info.JoinPermission = Constants.JoinOpen;
            return info;
        }
    }
}